//
// Copyright (c) 2004 BroadVision, Inc. All rights reserved.
//
// This software is copyrighted.  Under the copyright laws, this software
// may not be copied, in whole or in part, without prior written consent
// of BroadVision, Inc. or its assignees. This software is provided under
// the terms of a license between BroadVision and the recipient, and its
// use is subject to the terms of that license.
//
// This software may be protected by one or more U.S. and International
// patents. Certain applications of BroadVision One-To-One software are
// covered by U.S. patent 5,710,887.
//
// TRADEMARKS: BroadVision and the BroadVision logo are registered
// trademarks, and BroadVision One-To-One is a trademark of BroadVision,
// Inc. IONA and Orbix are trademarks of IONA Technologies, Ltd. RSA,
// MD5, and RC2 are trademarks of RSA Data Security, Inc. All other
// trademarks, service marks, and trade names belong to their respective
// owners. BroadVision, Inc. disclaims any proprietary interest in the
// marks and names of others.
//

var IE_CLIENT = 0;   // Microsoft Internet Explorer
var NS_CLIENT = 1;   // Netscape Navigator/Communicator

//
// Browser - Represents a browser object.
//
function Browser() {

  //
  this.client = _browser_getClient();
  this.version = _browser_getVersion();
  this.userAgent = _browser_getAgent();

  this.dom = document.getElementById ? 1 : 0;

  this.ns4 = (this.client == NS_CLIENT) && (this.version == 4);
  this.ns6 = (this.client == NS_CLIENT) && (this.version >= 5);
  this.ns7 = false;

  if (this.userAgent.toLowerCase().indexOf("netscape/7") != -1 && this.ns6) { 
    this.ns6 = false; 
    this.ns7 = true; 
  } 

  this.ie4 = (this.client == IE_CLIENT) && (this.version == 4);
  this.ie5 = (this.client == IE_CLIENT) && 
               (navigator.appVersion.indexOf("MSIE 5") > -1);
  this.ie6 = (this.client == IE_CLIENT) && 
               (navigator.appVersion.indexOf("MSIE 6") > -1);
}

//
// Browser::getClient - Returns the type of browser.
//
function _browser_getClient() {

  //
  var appName = navigator.appName;

  if ( appName.indexOf( 'Microsoft' ) != -1 )
    return IE_CLIENT;
  else if ( appName.indexOf( 'Netscape' ) != -1 )
    return NS_CLIENT;

  return -1;
}

//
// Browser::getAgent
//
function _browser_getAgent() {
  return navigator.userAgent;
}

//
// Browser::getVersion - Returns the major version of the browser.
//
function _browser_getVersion() {
  var version = navigator.appVersion;
  return version.substring( 0, 1 );
}

//
// Div - Represents a DIV element.  A Div is an
//       extension of the DOM Element and provides
//       the "glue" between the HTML/view and the
//       object model.
//
function Div(name, nest, dnest, ddnest, id) {
  this.element = null;

  // Get the named DIV element from the DOM
  if (!browser.ns4) {
    if (browser.dom) {
      this.element = document.getElementById(name);
    }
    else if (browser.ie4) {
      this.element = document.all[name];
    }
  }
  else {
    var tmp = 0;
    if (ddnest) {
      tmp = document[nest].document[dnest].document[ddnest].document[name];
    }
    else if (dnest) {
      tmp = document[nest].document[dnest].document[name];
    }
    else if (nest) {
      tmp = document[nest].document[name];
    }
    else {
      tmp = document.layers[name];
    }	
    this.element =  tmp ? tmp : 0;
  }

  if (!this.element) {
    alert('Error: DIV(' + name + ') does not exist.')
    return;
  }

  this.css = browser.dom || browser.ie4 ? this.element.style : this.element;  
  this.ref = browser.dom || browser.ie4 ? document : this.css.document;

  // Define the "methods"
  this.moveTo = _div__moveTo;
  this.show = _div__show;
  this.clipTo = _div__clipTo;
  this.setBackgroundColor = _div__setBackgroundColor;
  this.refresh = _div__refresh;

  this.x = 0;
  this.y = 0;
  this.width = 0;
  this.height = 0;

  this.clip = new Rect(0,0,0,0);

  this.id = id; 	
  this.obj = "g_DivObject" + name;

  eval(this.obj + " = this");

  return this;
}

//
// Div::moveTo - Move the DIV element.
//
// Parameters:
//
//   x
//   y
//
function _div__moveTo(x, y) {

  this.x = x;
  this.y = y;
  this.css.left = x;
  this.css.top = y;
}

//
// Div::show - Show/hide the DIV element.
//
// Parameters:
//
//   bVisible
//
function _div__show(bVisible) { 
  if (bVisible) {
    this.css.visibility = "visible";
  }
  else {
    this.css.visibility = "hidden";
  }
}

//
// Div::clipTo - Clip the DIV element.
//
// Parameters:
//
//   t top
//   r right
//   b bottom
//   l left
//
function _div__clipTo(t, r, b, l) {

  if (t < 0) t = 0;
  if (r < 0) r = 0;
  if (b < 0) b = 0;
  if (l < 0) l = 0;

  this.clip.top = t;
  this.clip.right = r;
  this.clip.bottom = b;
  this.clip.left = l;

  if (browser.ns4) {
    this.css.clip.top = t;
    this.css.clip.right = r;
    this.css.clip.bottom = b;
    this.css.clip.left = l;
  }
  else {
    this.css.clip = "rect(" + t + "," + r + "," + b +"," + l +")";
    this.css.width = r;
    this.css.height = b;
    this.width = r;
    this.height = b;
  }
}

//
// Div::setBackgroundColor
//
// Parameters:
//
//   rgb
//
function _div__setBackgroundColor(rgb) {
  if (browser.ns4) {
    this.css.bgColor = rgb;
  }
  else {
    this.css.backgroundColor = rgb;
  }
}

//
//
//
function _div__refresh(contents) {
  if (browser.dom) {
    this.element.innerHTML = contents;
  }
  else if (browse4.ie4) { 
    this.element.innerHTML = contents;
  }
  else if (browser.ns4) { 
    this.element.document.open();
    this.element.document.writeln(contents);
    this.element.document.close();
  }
}

//
// Popup - Represent a popup.
//
function Popup(name, anchor) {

  if (!window.listenerAttached) {
    window.listenerAttached = true;
    _popup__addListener();
  }

  var id = g_popups.length;
  g_popups[id] = this;

  // internal UID
  this._id = id;

  this.name = name;
  this.autoHide = true;
  this.contents = "";

  this.offsetX = 0;
  this.offsetY = 25;

  // Define the "methods"
  this.moveTo = _popup__moveTo;
  this.show = _popup__show;
  this.addListener = _popup__addListener;
  this.isClicked = _popup__isClicked;
  this.setAutoHide = _popup__setAutoHide;
  this.isAutoHide = _popup__isAutoHide;
  this.refresh = _popup__refresh;
  this.setContents = _popup__setContents;


  // DIV elements that make up a block

  this.container = new Div(name);

  if (anchor != null) {
    var pt = getAnchorPoint(anchor);
    this.moveTo(pt.x, pt.y);
  }

  this.obj = "g_PopupObject" + id;

  eval(this.obj + " = this");

  return this;
}

//
// Popup::moveTo - move the popup to a (x, y)
//
// Parameters:
//
//   x -
//   y -
//
function _popup__moveTo(x, y) {
  this.container.moveTo(x + this.offsetX, y + this.offsetY);
}

//
// Popup::show - show/hide the popup
//
// Parameters:
//
//   bVisible - Visibility flag (boolean)
//                true -
//                false -
//
function _popup__show(bVisible) {
  this.container.show(bVisible);
}

//
// Popup::addListener -
//
function _popup__addListener() {
  if (document.layers) {
    document.captureEvents(Event.MOUSEUP);
  }
  window.popupWindowOldEventListener = document.onmouseup;
  if (window.popupWindowOldEventListener != null) {
    document.onmouseup = new Function("window.popupWindowOldEventListener(); on_mouseup();");
  }
  else {
    document.onmouseup = on_mouseup;
  }
}

//
//
//
function _popup__isClicked(e) {
  if (browser.ns4) {
    var x = e.pageX;
    var y = e.pageY;
    var element = document.layers[this.name];
    if ((x > element.left) &&
        (x < element.left + element.clip.width) &&
        (y > element.top) &&
        (y < element.top + element.clip.height)) {
      return true;
    }
    else {
      return false;
    }
  }
  else if (browser.ie4) {
    var element = window.event.srcElement;
    while (element.parentElement != null) {
      if (element.id == this.name) {
        return true;
      }
      element = element.parentElement;
    }
    return false;
  }
  else if (browser.dom && e) {
    var element = e.originalTarget;
    while (element.parentNode != null) {
      if (element.id == this.name) {
        return true;
      }
      element = element.parentNode;
    }
    return false;
  }
  return false;
}

//
//
//
function _popup__setAutoHide(autoHide) {
  this.autoHide = autoHide;
}

//
//
//
function _popup__isAutoHide() {
  return this.autoHide;
}

//
//
//
function _popup__refresh() {
  this.container.refresh(this.contents);
}

//
//
//
function _popup__setContents(contents) {
  this.contents = contents;
}

//
// on_mouseup - onMouseUp event handler.
//
function on_mouseup(e) {
  for (var i = 0; i < g_popups.length; i++) {
    if (g_popups[i] != null) {
      var popup = g_popups[i];

      if (popup.isAutoHide() && !popup.isClicked(e)) {
        popup.show(false);
      }
    }
  }
}

//
// Rect - Represents a rectangle.
//
// Parameters:
//
//   t -
//   r -
//   b -
//   l -
//
function Rect(t, r, b, l) {

  //
  this.top = t;
  this.right = r;
  this.bottom = b;
  this.left = l;
}

//
// Point - Represents a point/coordinate.
//
function Point(p_x, p_y) {

  //
  this.x = p_x;
  this.y = p_y;
}

//
// Get the named DIV element from the DOM.
//
function getElementByName(name, nest, dnest, ddnest) {

  var element;

  if (!browser.ns4) {
    if (browser.dom) {
      element = document.getElementById(name);
    }
    else if (browser.ie4) {
      element = document.all[name];
    }
  }
  else {
    var tmp = 0;

    if (ddnest) {
      tmp = document[nest].document[dnest].document[ddnest].document[name];
    }
    else if (dnest) {
      tmp = document[nest].document[dnest].document[name];
    }
    else if (nest) {
      tmp = document[nest].document[name];
    }
    else {
      tmp = document.layers[name];
    }	
    element =  tmp ? tmp : 0;
  }

  return element;
}

//
//
//
function getAnchorPoint(anchor) {
  var x = 0;
  var y = 0;

  if (browser.ns4) {
    for (var index = 0; index < document.anchors.length; index++) {
      if (document.anchors[index].name == anchor) {
        x = document.anchors[index].x;
        y = document.anchors[index].y;
        break;
      }
    }
  }
  else {
    var element = getElementByName(anchor);
    if (element != null) {
      x = !browser.ns4 ? element.offsetLeft : element.pageX;
      y = !browser.ns4 ? element.offsetTop : element.pageY;
  
      if (!browser.ns4 && !browser.ns6) {
        while ((element = element.offsetParent) != null) {
          x += element.offsetLeft;
          y += element.offsetTop;
        }
      }
    }
  }

  var point = new Point(x, y);
  return point;
}

//
// Globals
//

  var browser = new Browser;
  var g_popups = new Array;
